<?php

namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Http\Controllers\Web\WebController;

use Illuminate\Support\Facades\Input;

use Session , Cookie , Config , Log , Validator;

use App\Models\User;
use App\Models\Msg;


class IndexController extends WebController
{

    private $_length;

    public function __construct(){
        parent::__construct();
        $this->_length = 20;
        $this->_response['_title']  = '';
        $this->_response['_css'] = [
            'index.css'
        ];

        $this->_response['active_bottom'] = 'index';
    }

    public function index(Request $request){

        $page = 1;

        if($request->has('page')){
            $page = $request->get('page');
        }

        $offset = (($page - 1) * $this->_length);

        $userModel = new User();
        $msgModel = new Msg();
        $msgs = Msg::select(
            $userModel->getTable().'.nick_name',
            $userModel->getTable().'.headimgurl',
            $userModel->getTable().'.wx_openid',
            $msgModel->getTable().'.id',
            $msgModel->getTable().'.time',
            $msgModel->getTable().'.address',
            $msgModel->getTable().'.num',
            $msgModel->getTable().'.img',
            $msgModel->getTable().'.remark',
            $msgModel->getTable().'.created_at'
        )->leftJoin($userModel->getTable() , $userModel->getTable() .'.id' , '=' , $msgModel->getTable().'.u_id')
            ->whereNull($msgModel->getTable().'.deleted_at')->orderBy($msgModel->getTable().'.created_at' , 'desc')
            ->skip($offset)->take($this->_length)
            ->get();

        foreach ($msgs as $msg){
            $msg->img = explode('|' , $msg->img);
        }

        $this->_response['active_bottom'] = 'index';

        $this->_response['msgs'] = $msgs;
        $this->_response['page'] = $page;

        if($request->ajax()){
            return response()->json(['code' => 0 , 'msg' => '成功' , 'data' => $this->_response]);
        }else{
            return view('web.index' , $this->_response);
        }

    }

    public function add(Request $request){

        $this->_response['active_bottom'] = 'index';

        $this->_response['time'] = date('Y-m-d H:i:s' , time());
        return view('web.add' , $this->_response);
    }

    public function doAdd(Request $request){
        $validation = Validator::make($request->all(), [
            'time'                          => 'required',
            'address'                       => 'required'
        ] , [
            'time.required'     => "时间必须填写",
            'address.required'  => "是必须填写的"
        ]);

        if($validation->fails()){
            $error = $validation->errors()->all();

            $this->_response['error'] = $error[0];
            $this->_response['time'] = date('Y-m-d H:i:s' , time());
            return view('web.add' , $this->_response);
        }

        $param = $request->all();

        if($param['num'] == '0'){
            $this->_response['error'] = '必须选择分类';
            $this->_response['time'] = date('Y-m-d H:i:s' , time());
            return view('web.add' , $this->_response);
        }

        global $uid;

        $param['u_id'] = $uid;
        $param['img'] = '';

        $files = [];

        $files[0] = $request->file('img1');

        $destinationPath	= public_path()."/upload/";

        foreach ($files as $file) {

            $filename			= uniqid();

            $exten = pathinfo($file->getClientOriginalName(),PATHINFO_EXTENSION);

//            $exten = substr($file->getClientOriginalName(), strpos($file->getClientOriginalName(), '.') + 1);

            if ($file->isValid()) {
                if ($file->move($destinationPath, $filename . '.' . $exten)) {
                    $filePath = "/upload/" . $filename . '.' . $exten;

                    $param['img'] .= $filePath ."|";
                }
            }
        }

        unset($param['img1']);

        if(Msg::create($param)){
            return redirect('/index');
        }else{
            $this->_response['error'] = '操作失败';
            $this->_response['time'] = date('Y-m-d H:i:s' , time());
            return view('web.add' , $this->_response);
        }

    }
}
